//
//  PhotosContainerView.m
//  SDAutoLayoutDemo
//
//  Created by gsd on 16/5/13.
//  Copyright © 2016年 gsd. All rights reserved.
//

#import "PhotosContainerView.h"
#import "UIView+SDAutoLayout.h"
#import "SDPhotoBrowser.h"

@interface PhotosContainerView()<SDPhotoBrowserDelegate>

@end


@implementation PhotosContainerView
{
    NSMutableArray *_imageViewsArray;
    NSMutableArray *_temp;
}

- (instancetype)initWithMaxItemsCount:(NSInteger)count
{
    if (self = [super init]) {
        self.maxItemsCount = count;
    }
    return self;
}

- (void)setPhotoNamesArray:(NSArray *)photoNamesArray
{
    _photoNamesArray = photoNamesArray;
    
    if (!_imageViewsArray) {
        _imageViewsArray = [NSMutableArray new];
    }
    
    int needsToAddItemsCount = (int)(_photoNamesArray.count - _imageViewsArray.count);
    
    if (needsToAddItemsCount > 0) {
        
        for (int i = 0; i < needsToAddItemsCount; i++) {
            UIImageView *imageView = [UIImageView new];
            [self addSubview:imageView];
            
            
            [_imageViewsArray addObject:imageView];

        }
    }
    
    _temp = [NSMutableArray new];
    
    [_imageViewsArray enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
        
        if (idx < _photoNamesArray.count) {
            
            
            imageView.hidden = NO;
            imageView.sd_layout.autoHeightRatio(1);
            
#pragma mark- 设置图片
            
//            NSString *url = [NSString stringWithFormat:@"%@%@",IMG_URL,[NSString stringIsNull:_photoNamesArray[idx]]];
//
//            [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"灰色_03"]];
            
            
         imageView.image = [UIImage imageNamed:_photoNamesArray[idx]];
            
            
            [_temp addObject:imageView];
            
            imageView.userInteractionEnabled = YES;
            imageView.tag = idx;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
            
            [imageView addGestureRecognizer:tap];
            
            
        } else {
            
            [imageView sd_clearAutoLayoutSettings];
            imageView.hidden = YES;
        }
        
        

    }];
    

    [self setupAutoWidthFlowItems:[_temp copy] withPerRowItemsCount:3 verticalMargin:10 horizontalMargin:10 verticalEdgeInset:0 horizontalEdgeInset:0];
    
}

#pragma mark - private actions

- (void)tapImageView:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag;
    
    browser.sourceImagesContainerView = self;
    
    browser.imageCount = _temp.count;
    
    browser.delegate = self;
    
    [browser show];
}
#pragma mark - SDPhotoBrowserDelegate

//- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
//{
//    NSString *imageName = self.photoNamesArray[index];
//    
//    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
//    
//    return url;
//}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imageView = _temp[index];
    
    return imageView.image;
}



@end
